package cn.edu.hpu.service;

import java.util.List;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.hpu.dao.ContactsDao;
import cn.edu.hpu.dao.Where;
import cn.edu.hpu.pojo.Contacts;
import cn.edu.hpu.util.Pager;
@Transactional
public class ContactsService{
	private ContactsDao contactsDao;
	
	public List<Contacts> findAllContacts(List<Where> wheres){
		return contactsDao.find(wheres);
	}
	public List<Contacts> findAll(){
		return contactsDao.findAll();
	}
	public void save(Contacts contacts){
		contactsDao.save(contacts);
	}
	public void delete(String id){
		contactsDao.del(id);
	}
	public Contacts findA(String id){
		return contactsDao.get(id);
	}
	public List<Contacts> findAllBy(String view){
		return contactsDao.findBy("view", view);
	}
	public long ccount(String userid){
		String hql = "select count(*) from Contacts where userid=? and enable=?";
		Object object = contactsDao.findUnique(hql, userid,true);
		return (Long)object;
	}
	public void update(Contacts contacts){
		contactsDao.update(contacts);
	}
	public List<Contacts> findByName(String name){
		return contactsDao.findBy("name", name);
	}
	public List<Contacts> findByCompanyname(String companyname){
		return contactsDao.findBy("companyname", companyname);
	}
	public List<Contacts> findByNameOrCompanyname(String name){
		return contactsDao.findByNameOrCompanyname(name);
	}
	public long findContactsCount(){
		String hql = "select count(*) from Contacts";
		Object object = contactsDao.findUnique(hql);
		return (Long)object;
	}
	@SuppressWarnings("unchecked")
	public Pager<Contacts> findByPager(int num){
		int totalcount = (int) findContactsCount();
		Pager<Contacts>  p = new Pager<Contacts>(num,totalcount);
		String hql = "from Contacts";
		Query  query = contactsDao.getSession().createQuery(hql);
		query.setFirstResult(p.getStartlinenum());
		query.setMaxResults(p.getPagesize());
		List<Contacts> list = query.list();
		p.setResult(list);
		return p;
	}
	//setter,getter
	@Autowired
	public void setContactsDao(ContactsDao contactsDao) {
		this.contactsDao = contactsDao;
	}
	
	
}
